from flask import Flask, render_template, request, escape
from zhiwen import right

app = Flask(__name__)

def log_request(req: 'flask_request', res: str) -> None:
    with open('vsearch.log', 'a') as log:
        print(req.form, req.remote_addr, req.user_agent, res, file=log, sep='|')

@app.route('/search4',methods=['POST'])
def do_search() -> 'html':
    phrase = request.form['phrase']
    title = '您的查询结果是：'
    results = right(phrase)
    result = ('对应内容是')
    return render_template('chenxi(2).html',
                           the_title=title,
                           the_row_titles=result,
                           the_data=results,)

@app.route('/')
@app.route('/chenxi.html')
def entry_page() -> 'html':
    return render_template('chenxi.html',
                           the_title='观手知天命')


@app.route('/viewlog')
def view_the_log() -> 'html':
    contents = []
    with open('vsearch.log') as log:
        for line in log:
            contents.append([])
            for item in line.split('|'):
                contents[-1].append(escape(item))
    titles = ('Form Data', 'Remote_addr', 'User_agent', 'Results')
    return render_template('viewlog.html',
                           the_title='View Log',
                           the_row_titles=titles,
                           the_data=contents,)


if __name__ == '__main__':
    app.run(debug=True)
